package com.kuang.controller;

import com.kuang.pojo.Books;
import com.kuang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author:liuwei
 * @creatdate:2022-04-11-10:32
 */
@Controller
@RequestMapping("/book")
public class BookController {
	@Autowired
	@Qualifier("BookServiceImpl")
	private BookService bookService;
	@RequestMapping("/")
	public String index(){
		return "index";
	}
	/**
	 *  查询所有的数据，并返回到数据展示页面
	 */
	@RequestMapping("/allBooks")
	public String list(Model model){
		List<Books> list = bookService.queryAllBook();
		model.addAttribute("list",list);
		return "allBook";
	}
	/**
	 * 跳转新增页面
	 */
	@RequestMapping("/toAddBook")
	public String toAddPaper() {
		return "addBook";
	}
	@RequestMapping("/addBook")
	public String addPaper(Books books) {
		System.out.println(books);
		bookService.addBook(books);
		return "redirect:/book/allBooks";
	}

	@RequestMapping("/toUpdateBook")
	public String toUpdateBook(Model model, int id) {
		Books books = bookService.queryBookById(id);
		System.out.println(books);
		model.addAttribute("book",books );
		return "updateBook";
	}

	@RequestMapping("/updateBook")
	public String updateBook(Model model, Books book) {
		System.out.println(book);
		bookService.updateBook(book);
		Books books = bookService.queryBookById(book.getBookID());
		model.addAttribute("books", books);
		return "redirect:/book/allBook";
	}
	@RequestMapping("/del/{bookId}")
	public String deleteBook(@PathVariable("bookId") int id) {
		bookService.deleteBookById(id);
		return "redirect:/book/allBook";
	}
}
